<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests\AdminLoginPostRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    //显示后台登录页面

    public function getLogin(){

    
        //显示模板
       return view('admin.login.login');
    }

    //后台登陆验证
    public function postCheck(AdminLoginPostRequest $request)
    {   

        
    
        // dd($request->all());
        //检测用户是否存在
         $user = DB::table('user')->where('username',$request->input('username'))->first();
         // dd($user);
         if(!$user){
            return back()->with('error','用户名或密码错误');
         }
        //dd($user);

         //检测密码是否一致
         if(Hash::check($request->input('password'),$user->password)){

          
        }else{
            //失败
            return back()->with('error','用户名或密码错误');
         }
            
           if($user->qx==1){
                 session(['id'=>$user->id]);
            //跳转到后台首页
            return redirect('/admin')->with('success','欢迎'.$user->username.'进入后台');
            }else{
            //失败
            return back()->with('error','不是管理员');
         }
    }

    //后台退出

    public function getLogout(){
        // echo '222';
        session()->forget('id');
        return redirect('/admin');
    }
}
